﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SimulChess
{
    public enum PieceColor
    {
        white,
        black
    }
    
    public enum PieceName
    {
        pawn,
        knight,
        bishop,
        rook,
        queen,
        king
    }
    
    public enum Movement
    {
        allowed,
        capture,
        both,
        allowedOnce,
        blocked
    }

    public class Game
    {
        public ChessBoard Board;
        public List<ChessPiece> PiecesWhite;
        public List<ChessPiece> PiecesBlack;

        public Game()
        {
            this.Setup();
        }

        public ChessPiece FindPieceAtPosition(int X, int Y)
        {
            foreach (ChessPiece p in this.PiecesWhite)
                if ((p.PositionX == X) && (p.PositionY == Y))
                    return p;

            foreach (ChessPiece p in this.PiecesBlack)
                if ((p.PositionX == X) && (p.PositionY == Y))
                    return p;

            return null;
        }

        private void Setup()
        {
            this.Board = new ChessBoard();

            this.PiecesWhite = this.SetupPieces(PieceColor.white);

            this.PiecesBlack = this.SetupPieces(PieceColor.black);
        }

        private List<ChessPiece> SetupPieces(PieceColor Color)
        {
            // Default to white color
            int yPawn = 6;
            int yOther = 7;

            if (Color == PieceColor.black)
            {
                yPawn = 1;
                yOther = 0;
            }

            List<ChessPiece> pieces = new List<ChessPiece>()
            {
                new Rook(Color,   0, yOther),
                new Knight(Color, 1, yOther),
                new Bishop(Color, 2, yOther),
                new Queen(Color,  3, yOther),
                new King(Color,   4, yOther),
                new Bishop(Color, 5, yOther),               
                new Knight(Color, 6, yOther),
                new Rook(Color,   7, yOther),
                new Pawn(Color,   0, yPawn),
                new Pawn(Color,   1, yPawn),
                new Pawn(Color,   2, yPawn),
                new Pawn(Color,   3, yPawn),
                new Pawn(Color,   4, yPawn),
                new Pawn(Color,   5, yPawn),
                new Pawn(Color,   6, yPawn),
                new Pawn(Color,   7, yPawn)
            };

            this.Board.Position[0, yOther].Occupied = true;
            this.Board.Position[1, yOther].Occupied = true;
            this.Board.Position[2, yOther].Occupied = true;
            this.Board.Position[3, yOther].Occupied = true;
            this.Board.Position[4, yOther].Occupied = true;
            this.Board.Position[5, yOther].Occupied = true;
            this.Board.Position[6, yOther].Occupied = true;
            this.Board.Position[7, yOther].Occupied = true;
            this.Board.Position[0, yPawn].Occupied = true;
            this.Board.Position[1, yPawn].Occupied = true;
            this.Board.Position[2, yPawn].Occupied = true;
            this.Board.Position[3, yPawn].Occupied = true;
            this.Board.Position[4, yPawn].Occupied = true;
            this.Board.Position[5, yPawn].Occupied = true;
            this.Board.Position[6, yPawn].Occupied = true;
            this.Board.Position[7, yPawn].Occupied = true;

            return pieces;
        }
    }
}